import pygame,pygame.locals
import sys,os,time

# 2021-05-20 15:17 BY 炸天帮成员
# 本文件综合参考自 _3_pygame_demo_Part2_OK.py 
# _6_pygame_demo_colorBlock_1_moving.py
# 本质：在_6_pygame_demo_colorBlock_1_moving.py的基础上，改用类继承实现。
# 展望：下一个版本将会使  COLOR_BLOCK对应note号，而 SHADOW_BLOCK对应非想要的note号。应该会存在mido的文件夹中。

COLOR_BLACK = (0,0,0)
COLOR_WHITE = (255,255,255)
COLOR_GREEN = (0,255,0)
COLOR_BKG = COLOR_BLACK

def game_init():
    pygame.init()
    pygame.display.init()

WINDOW_MAX_X = 400
WINDOW_MAX_Y = 400
DISPLAYSURFACE = pygame.display.set_mode((WINDOW_MAX_X,WINDOW_MAX_Y))
DISPLAYSURFACE.fill(COLOR_BKG)    #   pygame的背景颜色

#   -----------------------------------------------------------------------------------------------
#   以下为COLOR_BLOCK和SHADOW_BLOCK的类继承，其实可以拆开成2个类。类似于_3_pygame_demo_Part2_OK.py中的Player和Enemy两个类一样。
COLOR_BLOCK_X = 40
COLOR_BLOCK_Y = 40
COLOR_BLOCK_POSITION = (200,200)
BLOCK_MOVE_STEP_X = -5
NEED_REFRESH = False
#   以上为需要在 类中使用的基本参数 ，尽量不要在类中出现具体数字，类变大后很难修改。
class IRO_BLOCK(pygame.sprite.Sprite):
    def __init__(self,BLOCK_TYPE,BLOCK_ID,BLOCK_COLOR):
        super().__init__()  #   注意这里的super后需要带上括号。
        if BLOCK_TYPE == 'COLOR_BLOCK':
            if BLOCK_ID == 0:
                #self.image = pygame.image.load(" ")
                self.BLOCK_X = COLOR_BLOCK_X
                self.BLOCK_Y = COLOR_BLOCK_Y
                self.BLOCK_POSITION = COLOR_BLOCK_POSITION
                self.BLOCK_SURF = pygame.Surface((self.BLOCK_X,self.BLOCK_Y))
                self.BLOCK_RECT = self.BLOCK_SURF.get_rect(center=self.BLOCK_POSITION) 
                self.BLOCK_SURF.fill(BLOCK_COLOR)
        elif BLOCK_TYPE == 'SHADOW_BLOCK':
            if BLOCK_ID == 0:
                #self.image = pygame.image.load(" ")
                self.BLOCK_X = COLOR_BLOCK_X
                self.BLOCK_Y = COLOR_BLOCK_Y
                self.BLOCK_POSITION = (COLOR_BLOCK_POSITION[0]+COLOR_BLOCK_X,COLOR_BLOCK_POSITION[1])
                self.BLOCK_SURF = pygame.Surface((self.BLOCK_X,self.BLOCK_Y))
                self.BLOCK_RECT = self.BLOCK_SURF.get_rect(center=self.BLOCK_POSITION) 
                self.BLOCK_SURF.fill(BLOCK_COLOR)
    def load(self):
        DISPLAYSURFACE.blit(self.BLOCK_SURF,self.BLOCK_RECT)

    def move(self,BLOCK_TYPE):
        global NEED_REFRESH
        self.load()
        self.BLOCK_RECT.move_ip(BLOCK_MOVE_STEP_X,0)
        if BLOCK_TYPE == 'COLOR_BLOCK':
            if self.BLOCK_RECT.right < 0:
                self.BLOCK_RECT.left = WINDOW_MAX_X
                NEED_REFRESH = True
            else :
                NEED_REFRESH = False
        elif BLOCK_TYPE == 'SHADOW_BLOCK' and NEED_REFRESH == True:
            self.BLOCK_RECT.left = WINDOW_MAX_X + COLOR_BLOCK_X
#   --------------------------------------------------------------------------------------
if __name__ == "__main__":
    game_init()
    FPS = 60
    COLOR_BLOCK_0 = IRO_BLOCK('COLOR_BLOCK',0,COLOR_GREEN)
    SHADOW_BLOCK_0 = IRO_BLOCK('SHADOW_BLOCK',0,COLOR_BKG)

    while True:
        pygame.time.Clock().tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.locals.QUIT:
                pygame.quit()
                sys.exit()

        COLOR_BLOCK_0.move('COLOR_BLOCK')
        SHADOW_BLOCK_0.move('SHADOW_BLOCK')

        pygame.display.update()